using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Safira.Spome.WebServices.Messages;
using System.Globalization;
using Safira.Spome.WebServices.Properties;
using Safira.Tools.Sharepoint.Spome;
using Safira.Spome.WebServices.Types;
using Microsoft.SharePoint;

namespace Safira.Spome.WebServices
{
    /// <summary>
    /// Summary description for SpomeServices
    /// </summary>
    [WebService( Namespace = "http://www.safira.pt/Spome/" )]
    [WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 )]
    [ToolboxItem( false )]
    public class SpomeServices : System.Web.Services.WebService
    {
        /// <summary>
        /// Loads the site.
        /// </summary>
        /// <param name="LoadSiteRequest">The load site request.</param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "myWeb" ), 
        System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes" ), 
        System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "LoadSiteRequest" ), 
        WebMethod( Description = "Get all site information" )]
        public LoadSiteResponse LoadSite( LoadSiteRequest LoadSiteRequest )
        {
            LoadSiteResponse resp = new LoadSiteResponse();

            #region Validation

            if ( LoadSiteRequest == null )
            {
                resp.Success = false;
                return resp;
            }

            #endregion

            try
            {
                ESPWeb myWeb    = new ESPWeb( LoadSiteRequest.Url );
                resp.Site       = new SharepointSite();

                //resp.Site.Title = myWeb.Title;
                //resp.Site.Url   = myWeb.Url;

                resp.Site.Lists = GetLists( myWeb );
                resp.Site.Pages = GetPages( myWeb );

                resp.Site.MasterPageGallery = GetMasterPageGallery( myWeb );

                #region Getting Child Webs

                ESPWeb[] webs = myWeb.GetAllESPWebs();

                if ( webs != null )
                {
                    resp.Site.Webs = new SharepointWeb[ webs.Length ];

                    for ( int i = 0; i < webs.Length; i++ )
                    {
                        resp.Site.Webs[ i ] = DigestWeb( webs[ i ] );
                    }
                }

                #endregion

                resp.Success = true;
            }
            catch(Exception e)
            {
                resp.Success = false;
                resp.Trace = string.Format( CultureInfo.InvariantCulture, "<Message>{0}</Message><StackTrace>{1}</StackTrace>", e.Message, e.StackTrace );
            }
                

            return resp;
        }

        /// <summary>
        /// Pings the specified ping request.
        /// </summary>
        /// <param name="PingRequest">The ping request.</param>
        /// <returns></returns>
        [WebMethod( Description = "Checks if the webservices are up and running." )]
        public PingResponse Ping( PingRequest PingRequest )
        {
            PingResponse resp = new PingResponse();
            resp.Moment = DateTime.UtcNow;
            resp.Message = string.Format( CultureInfo.InvariantCulture, Resources.Ping_Hello, PingRequest.Name );

            return resp;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods" ),
        WebMethod( Description = "Uploads content to an existing file" )]
        public FileUploadResponse UploadFile( FileUploadRequest request )
        {
            ESPWeb web = new ESPWeb( request.Url );
            ESPList list = new ESPList( web.Lists[ request.List ] );

            FileUploadResponse response = new FileUploadResponse();

            response.Success = list.UploadFile( request.Content, request.Filename);

            return response;
        }

        #region Private Methods

        /// <summary>
        /// Digests the web.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <returns></returns>
        private static SharepointWeb DigestWeb( ESPWeb web )
        {
            SharepointWeb myWeb = new SharepointWeb();

            //myWeb.Title = web.Title;
            //myWeb.Url   = web.Url;

            myWeb.Lists = GetLists( web );
            myWeb.Pages = GetPages( web );

            #region Getting Child Webs

            ESPWeb[] webs = web.GetAllESPWebs();

            if ( webs != null )
            {
                myWeb.Webs = new SharepointWeb[ webs.Length ];

                for ( int i = 0; i < webs.Length; i++ )
                {
                    myWeb.Webs[ i ] = DigestWeb( webs[ i ] );
                }
            }

            #endregion

            return myWeb;        
        }

        /// <summary>
        /// Gets the pages.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "web" )]
        private static SharepointPage[] GetPages( ESPWeb web )
        {
            return null;
        }

        /// <summary>
        /// Gets the lists.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "web" )]
        private static SharepointList[] GetLists( ESPWeb web )
        {
            ESPList[] lists = web.GetAllESPLists();
            
            if (lists == null)
            {
                return null;
            }

            SharepointList[] myLists = new SharepointList[ lists.Length ];

            for ( int i = 0; i < lists.Length; i++ )
            {
                myLists[ i ] = DigestList( lists[i] );
            }

            return null;
        }

        /// <summary>
        /// Digests the list.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <returns></returns>
        private static SharepointList DigestList( ESPList list )
        {
            SharepointList myList = new SharepointList();

            myList.Name = list.MyList.Title;

            return myList;
        }

        /// <summary>
        /// Gets the master page gallery.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <returns></returns>
        private static SharepointPageLayout[] GetMasterPageGallery( ESPWeb web )
        {
            ESPPageLayout[] list = web.GetMasterPageGallery();

            if ( list.Length <= 0 )
            {
                return null;
            }

            SharepointPageLayout[] myList = new SharepointPageLayout[ list.Length ];

            for ( int i = 0; i < list.Length; i++ )
            {
                myList[ i ] = new SharepointPageLayout();

                myList[ i ].Name = list[ i ].Name;
                myList[ i ].ContentType = list[ i ].ContentType;
            }

            return null;
        }

        #endregion
    }
}
